package com.zqj.project.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.common.database.mybatis.conditions.Wraps;
import com.zqj.project.common.exception.BizException;
import com.zqj.project.converter.OrderDetailConverter;
import com.zqj.project.entity.OrderDetailPO;
import com.zqj.project.factory.OrderDetailFactory;
import com.zqj.project.model.OrderDetail;
import com.zqj.project.repository.OrderDetailMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Description: OrderDetailServiceImpl
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/11/8 15:39
 * @Version: 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderDetailServiceImpl implements OrderDetailService {

    private final OrderDetailMapper orderDetailMapper;

    @Override
    public IPage<OrderDetail> page(OrderDetail orderDetail, IPage<OrderDetail> page) {
        final Long count = this.orderDetailMapper.selectCount(OrderDetailFactory.lbqWrapper(orderDetail));
        if (Objects.equals(0L, count)) {
            return page;
        }
        page.setTotal(count);
        return page.setRecords(OrderDetailConverter.toOrderDetailModelList(this.orderDetailMapper.selectPage(new Page<>(page.getCurrent(), page.getSize(), page.getTotal()), OrderDetailFactory.lbqWrapper(orderDetail)).getRecords()));
    }

    @Override
    public OrderDetail info(Long id) {
        OrderDetailPO orderDetailPo = this.orderDetailMapper.selectById(id);
        if (Objects.isNull(orderDetailPo)) {
            throw new BizException("订单明细不存在");
        }
        return OrderDetailConverter.toOrderDetailModel(orderDetailPo);
    }

    @Override
    public List<OrderDetail> list(List<Long> orderIdList) {
        return OrderDetailConverter.toOrderDetailModelList(this.orderDetailMapper.selectList(Wraps.<OrderDetailPO>lbQ().in(OrderDetailPO::getOrderId, orderIdList)));
    }

    @Override
    public List<OrderDetail> list(Long orderId) {
        return OrderDetailConverter.toOrderDetailModelList(this.orderDetailMapper.selectList(Wraps.<OrderDetailPO>lbQ().eq(OrderDetailPO::getOrderId, orderId)));
    }

    @Override
    public Boolean add(OrderDetail orderDetail) {
        return this.orderDetailMapper.insert(OrderDetailConverter.toOrderDetailPo(orderDetail)) == 1;
    }

    @Override
    public Boolean edit(OrderDetail orderDetail) {
        return this.orderDetailMapper.updateById(OrderDetailConverter.toOrderDetailPo(orderDetail)) == 1;
    }

    @Override
    public Boolean delete(Long id) {
        return this.orderDetailMapper.deleteById(id) == 1;
    }

}